


//
//  LqkinDataPicker.m
//  LqkinDataPicker
//
//  Created by 微我网络 on 15/11/5.
//  Copyright (c) 2015年 lamcelot. All rights reserved.
//

#define molove_bg_color 0xfd77a9
#define molove_bg_lrcolor 0xf8b5ce
#define molove_bg_wcolor 0xf3f3f3
#define molove_bg_lightgray 0xf3f3f3
#define molove_text_light_gray 0xb4b3b3
#define molove_bg_bluecolor 0x5fb1fd
#define ScreenHeight [UIScreen mainScreen].bounds.size.height

//获取设备的物理宽度
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#import "LqkinDataPicker.h"
#import "WW_viewtool.h"
@interface LqkinDataPicker ()
{
    UIView *bgview;
    UIDatePicker *_datapicker;
    UIButton *_surebtn;
}
@property (strong, nonatomic) NSDate *selectDate;
@end


@implementation LqkinDataPicker



-(instancetype)initWithFrame:(CGRect)frame type:(NSInteger)type
{
    self=[super initWithFrame:frame];
    if (self) {
        _tyep = type;
        [self initview];
    }
    return self;
}
- (void)initview{
    self.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    UITapGestureRecognizer *viewtap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTapped:)];
    [self addGestureRecognizer:viewtap];

    bgview=[[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-ScreenHeight/5-40, ScreenWidth, ScreenHeight/5)];
    bgview.backgroundColor=UIColorFromRGB(molove_bg_lightgray);
    _datapicker=[[UIDatePicker alloc] initWithFrame:CGRectMake(0, -(CGRectGetHeight(bgview.frame)-40)/2, ScreenWidth, 200)];
//    _datapicker.timeZone=[NSTimeZone timeZoneWithName:@"GMT"];
    _datapicker.backgroundColor=[UIColor whiteColor];
    self.clipsToBounds=YES;
    [bgview addSubview:_datapicker];

    
    UILabel *line=[WW_viewtool createLine:CGRectMake(0, CGRectGetMaxY(bgview.frame), ScreenWidth, 0.5)];
    _surebtn=[[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(line.frame), ScreenWidth, 40)];
    [_surebtn setTitle:@"确定" forState:UIControlStateNormal];
    [_surebtn setTitleColor:UIColorFromRGB(molove_bg_color) forState:UIControlStateNormal];
    [_surebtn addTarget:self action:@selector(btnsure) forControlEvents:UIControlEventTouchDown];
    _surebtn.titleLabel.font=[UIFont systemFontOfSize:15];
    _surebtn.backgroundColor=[UIColor whiteColor];
    _surebtn.titleLabel.textAlignment=NSTextAlignmentCenter;
    
    if (_tyep == 1) {
         [_datapicker setDatePickerMode:UIDatePickerModeDate];
    }
    else
    {
         [_datapicker setDatePickerMode:UIDatePickerModeTime];
    }

//    NSDateFormatter *formatter_minDate = [[NSDateFormatter alloc] init];
//    [formatter_minDate setDateFormat:@"yyyy-MM-dd hh:mm"];
//    [formatter_minDate setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
//    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//    [dateFormatter setDateFormat:@"yyyy"];
//    NSString *year1 = [dateFormatter stringFromDate:[NSDate date]];
//#warning - 修改18-》19
//#pragma mark - 修改年龄
//    NSString *year = [NSString stringWithFormat:@"%ld",[year1 integerValue] - 19];
//    NSDate *minDate1 = [formatter_minDate dateFromString:[NSString stringWithFormat:@"%@%@",year,@"-12-31:00:00"]];
//    NSDate *minDate = [formatter_minDate dateFromString:[NSString stringWithFormat:@"%ld%@",[year1 integerValue]- 99,@"-1-1:00:00"]];
   [_datapicker addTarget:self action:@selector(datePickerValueChange:) forControlEvents:UIControlEventValueChanged];
//     [_datapicker setMaximumDate:[NSDate date]];
//    [_datapicker setMaximumDate:minDate1];
    _selectDate = [NSDate date];
    _datapicker.date = _selectDate;
    _datapicker.minimumDate = _selectDate;
    if (_tyep == 1) {
        _datapicker.datePickerMode = UIDatePickerModeDate;
    }
    else
    {
       _datapicker.datePickerMode = UIDatePickerModeTime;
    }

    [self addSubview:bgview];
    [self addSubview:line];
    [self addSubview:_surebtn];
}

- (void)btnsure
{
    NSDate *date=_selectDate;
    
    NSLog(@"%@",_datapicker.date);
    //转换为世间戳
    NSString * timestampString = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy"];
    
    NSString *currentyearString = [NSString stringWithFormat:@"%@",
                                   [formatter stringFromDate:date]];
    
    [formatter setDateFormat:@"MM"];
    
    NSString *currentMonthString = [NSString stringWithFormat:@"%ld",[[formatter stringFromDate:date]integerValue]];
    [formatter setDateFormat:@"dd"];
    NSString *currentDateString = [NSString stringWithFormat:@"%@",[formatter stringFromDate:date]];
    
    [formatter setDateFormat:@"HH"];
    NSString * currentDateHourString =  [NSString stringWithFormat:@"%@",[formatter stringFromDate:date]];
    [formatter setDateFormat:@"mm"];
    NSString * currentDateMinuteString = [NSString stringWithFormat:@"%@",[formatter stringFromDate:date]];
    if (_delegate) {
        [_delegate LqkinPikerViewDelegateDateyear:currentyearString dmonth:currentMonthString dday:currentDateString hour:currentDateHourString minute:currentDateMinuteString timestamp:timestampString];
    }
    [self removeFromSuperview];
}

-(void)backgroundTapped:(UITapGestureRecognizer *)sender{
    CGPoint point = [sender locationInView:self];
    if (point.y<CGRectGetMaxY(bgview.frame)) {
        [self removeFromSuperview];
    }
}

- (void)datePickerValueChange:(id)sender
{
      _selectDate = [sender date];
}
@end
